﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Vehicle.BLL;
using Yipusoft.Vehicle.Model;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;
using Yipusoft.BasicBase.Model;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.SoldierArchives.BLL;

public partial class BasicBase_Vehicle_VehicleView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200600") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200606"))
            {
                Response.Write("<script>alert('没有权限！');window.close();</script>");
                return;
            }
            init();
        }

    }

    private void init()
    {
        VehicleInfo info = Vehicle.GetDetail(new Guid(Request.QueryString["id"]));

        if (info.Picture != "")
        {
            Picture.Text = "<a href=ViewPicture.aspx?FileID=" + info.Picture + " target=_blank title=点击查看原图><img src='pics/" + info.Picture + "' width='480px' /></a>";
        }
        Address.Text = info.Address;
        AlertInfo.Text = info.AlertInfo;
        CapacityOfFoam.Text = DecimalToString(info.CapacityOfFoam);
        CapacityOfPowder.Text = DecimalToString(info.CapacityOfPowder);
        CapacityOfWater.Text = DecimalToString(info.CapacityOfWater);
        ChassisType.Text = info.ChassisType;
        ConstantPressure.Text = DecimalToString(info.ConstantPressure);
        DateProduc.Text = DateTimeToString(info.DateProduc);
        DatePurchase.Text = DateTimeToString(info.DatePurchase);
        //Demension.Text = info.Demension;
        lblDepartment.Text = getDepartmentName(info.DepartmentID);
        lblClass.Text = GetClass(info.VehicleClass);
        NumberPlate.Text = info.NumberPlate;
        EngineType.Text = info.EngineType;
        //FlowRate.Text = DecimalToString(info.FlowRate);
        Life.Text = IntToString(info.Life);
        Manufacturer.Text = info.Manufacturer;
        MaxPower.Text = DecimalToString(info.MaxPower);
        MaxSpeed.Text = DecimalToString(info.MaxSpeed);
        MaxTorque.Text = DecimalToString(info.MaxTorque);
        MostUse.Text = info.MostUse;
        OtherParameters.Text = info.OtherParameters;
        Passenger.Text = IntToString(info.Passenger);
        Phone.Text = info.Phone;
        Price.Text = DecimalToString(info.Price);
        //State.Text = GetState(info.State);

        TotalWeight.Text = DecimalToString(info.TotalWeight);
        UseMethod.Text = info.UseMethod;

        VehicleMode.Text = info.VehicleMode;
        VehicleName.Text = info.VehicleName;

        TimeLastModified.Text = info.TimeLastModified.ToString();
        WorkHeight.Text = DecimalToString(info.WorkHeight);

        Wlan.Text = Convert.ToBoolean(info.Wlan) ? "是" : "否";
        VehicleWidth.Text = DecimalToString(info.VehicleWidth);
        VehicleLength.Text = DecimalToString(info.VehicleLength);
        VehicleHeight.Text = DecimalToString(info.VehicleHeight);
        //soldierCount.Text = IntToString(info.soldierCount);
        RadioNO.Text = info.RadioNO;
        PumpType.Text = info.PumpType;
        PumpFlux.Text = DecimalToString(info.PumpFlux);
        MaxDrinkDepth.Text = DecimalToString(info.MaxDrinkDepth);
        Number.Text = info.Number;
        BindVehicleDeviceds();
        BindGrid();

        if (info.VehicleClass == "101008" || info.VehicleClass == "102006" || info.VehicleClass == "102007" || info.VehicleClass == "102003" || info.VehicleClass.Substring(0, 4) == "1030")
        {
            DriverTr.Visible = true;
            string DriverWhere = "VehicleId='" + info.VehicleID + "'";
            VehicleDriverInfo DriverInfo = VehicleDriver.GetList(DriverWhere, 0, 1000).FirstOrDefault();
            if (DriverInfo != null)
            {
                Soldier soldier = new Soldier();
                lblDriver.Text = soldier.GetSoldierInfoByID(DriverInfo.SoldierId.ToString()).SoldierName;

            }
            else
            {
                lblDriver.Text = "";
            }

        }
        else
        {
            DriverTr.Visible = false;
        }

    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindVehicleDeviceds();
    }

    private void BindVehicleDeviceds()
    {
        int Count = VehicleDevice.GetCount(Request.QueryString["id"]);
        List<VehicleDeviceInfo> list = VehicleDevice.GetList(Count, 0, Request.QueryString["id"]);
        GridView1.DataSource = list;
        GridView1.DataBind();
    }

    string DateTimeToString(DateTime dt)
    {
        return dt.Year == 9999 ? "" : dt.ToString("yyyy-MM-dd");
    }

    string DecimalToString(Decimal de)
    {
        return de == -1 ? "" : de.ToString();
    }

    string IntToString(int data)
    {
        return data == -1 ? "" : data.ToString();
    }

    protected string getDepartmentName(string id)
    {
        return Department.GetDepartment(id).FullName;
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

            #region 赋值
            VehicleDeviceInfo info = e.Row.DataItem as VehicleDeviceInfo;
            DeviceInfo deviceInfo = info.DeviceInfo;
            Label DeviceName = e.Row.FindControl("DeviceName") as Label;
            DeviceName.Text = deviceInfo.DeviceName;

            Label DeviceClass = e.Row.FindControl("DeviceClass") as Label;
            DeviceClass.Text = GetCodeItem(deviceInfo.DeviceClass);

            Label State = e.Row.FindControl("State") as Label;
            State.Text = GetCodeItem(deviceInfo.State);

            #endregion
        }
    }

    public string GetCodeItem(string deviceClass)
    {
        Yipusoft.Common.Model.CodeItemInfo info = CodeItem.GetCodeItem(deviceClass);
        return info != null ? info.ItemName : "";
    }

    public string GetClass(string classID)
    {
        Yipusoft.Common.Model.CodeItemInfo info = CodeItem.GetCodeItem(classID, "XFZBLB");
        if (info != null)
        {
            return info.ItemName;
        }
        else
        {
            info = CodeItem.GetCodeItem(classID);
            if (info != null)
            {
                return info.ItemName;
            }
            else
            {
                return "";
            }
        }
    }

    public string GetState(string State)
    {
        Yipusoft.Common.Model.CodeItemInfo info = CodeItem.GetCodeItem(State, "XFCLZT");
        if (info != null)
        {
            return info.ItemName;
        }
        else
        {
            info = CodeItem.GetCodeItem(State);
            if (info != null)
            {
                return info.ItemName;
            }
            else
            {
                return "";
            }
        }
    }

    private void BindGrid()
    {
        List<AttachmentInfo> atta = Attachment.GetList("ItemID='" + Request.QueryString["id"] + "'");

        UserGrid.DataSource = atta;
        UserGrid.DataBind();
    }
    protected void UserGrid_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
}
